package com.swss.g.assets.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.swss.g.assets.bean.Asset;
import com.swss.g.assets.bean.AstFinAudit;
import com.swss.g.assets.dao.AssetDAO;
import com.swss.g.base.impl.BaseDAOImpl;
import com.swss.g.user.bean.User;
import com.swss.util.DoDate;

public class AssetDAOImpl extends BaseDAOImpl<Asset> implements AssetDAO {
    
	/* 财务审核
	 * (non-Javadoc)
	 * @see com.swss.g.assets.dao.AssetDAO#updateAstFinAudit(java.util.List, com.swss.g.assets.bean.AstFinAudit)
	 */
	public boolean updateAstFinAudit(final List<Asset> assetList,
			final AstFinAudit astFinAudit) throws Exception {
		// TODO Auto-generated method stub

		Boolean flag = (Boolean) this.getHibernateTemplate().execute(
				new HibernateCallback() {

					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						try {
							
							HttpServletRequest request = ServletActionContext.getRequest();
							HttpSession sessions = request.getSession();
							User u = (User) sessions.getAttribute("user");
							
							Date dateStr = new DoDate().getDate(new Date());
							
							for (Asset ast : assetList) {
								
								Asset asset = (Asset) session.load(Asset.class,ast.getId());
								asset.setFinVoucher(astFinAudit.getFinVoucher().trim());
								asset.setFinAccount(astFinAudit.getFinAccount().trim());
								asset.setJfly(astFinAudit.getJfly().trim());
								asset.setFinAdvice(astFinAudit.getFinAdvice().trim());
								asset.setAduit(5); //财务已通过
								asset.setFinAdtDate(dateStr);
								asset.setFinAdtName(u.getAccperName().trim());

								session.update(asset);
								session.flush();
								session.clear();
							}
							return true;
						} catch (Exception e) {
							e.printStackTrace();
							return false;
						}
					}

				});

		return flag;
	}

	@SuppressWarnings("unchecked")
	public boolean backAstFinAudit(List<Asset> assetList,String returreas) throws Exception {
		// TODO Auto-generated method stub
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpSession sessions = request.getSession();
			User u = (User) sessions.getAttribute("user");
			
			Date dateStr = new DoDate().getDate(new Date());
			List<Asset> backFinAuditAll  = new ArrayList();
			
			for (Asset ast : assetList) {
				Asset asset = (Asset) this.getHibernateTemplate().load(Asset.class, ast.getId());
				asset.setFinVoucher("");  //财务凭单
				asset.setFinAccount("0");  //财务账户
				asset.setJfly("0");  //经费来源
				asset.setFinAdvice("");  //财务意见
				asset.setFinAdtDate(new DoDate().getStrToDate("1990-1-1")); //审核日期
				asset.setFinAdtName("");  //审核人
				asset.setReturperson(u.getAccperName().trim()); //退回人
				asset.setRetureas(returreas); //退回原因
				asset.setReturtime(dateStr);
				asset.setAduit(0); //0财务退回
				
				backFinAuditAll.add(asset);
				
			}
			   this.saveOrUpdateAll(backFinAuditAll);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

		return true;
	}
    /**
     * 财务恢复
     */
	@SuppressWarnings("unchecked")
	public boolean throughAstFinAudit(List<Asset> assetList) throws Exception {
		// TODO Auto-generated method stub
		try {
			
			List<Asset> throughFinAuditAll  = new ArrayList();
			
			for (Asset ast : assetList) {
				Asset asset = (Asset) this.getHibernateTemplate().load(Asset.class, ast.getId());
				asset.setFinVoucher("");  //财务凭单
				asset.setFinAccount("0");  //财务账户
				asset.setJfly("0");  //经费来源
				asset.setFinAdvice("");  //财务意见
				asset.setAduit(3); //3 财务未审状态
				asset.setFinAdtDate(new DoDate().getStrToDate("1990-1-1")); //审核日期
				asset.setFinAdtName("");  //审核人
				
				
				throughFinAuditAll.add(asset);
				
			}
			   this.saveOrUpdateAll(throughFinAuditAll);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

		return true;
	}
    /**
     * 资产终审
     */
	public boolean astLastAuditUpdate(final List<Asset> assetList) throws Exception {
		// TODO Auto-generated method stub
		Boolean flag = (Boolean) this.getHibernateTemplate().execute(
				new HibernateCallback() {

					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						try {
							for (Asset ast : assetList) {
								
								Asset asset = (Asset) session.load(Asset.class,ast.getId());
								asset.setAduit(7);
								asset.setRecordDate(new DoDate().getDate(new Date()));
								session.update(asset);
								session.flush();
								session.clear();
							}
							return true;
						} catch (Exception e) {
							e.printStackTrace();
							return false;
						}
					}

				});

		return flag;
	}
    /**
     * 终审退回
     */
	@SuppressWarnings("unchecked")
	public boolean backAstLastAudit(List<Asset> assetList, String returreas)
			throws Exception {
		// TODO Auto-generated method stub
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpSession sessions = request.getSession();
			User u = (User) sessions.getAttribute("user");
			
			Date dateStr = new DoDate().getDate(new Date());
			List<Asset> backFinAuditAll  = new ArrayList();
			
			for (Asset ast : assetList) {
				Asset asset = (Asset) this.getHibernateTemplate().load(Asset.class, ast.getId());
				
				asset.setFinVoucher("");  //财务凭单
				asset.setFinAccount("0");  //财务账户
				asset.setJfly("0");  //经费来源
				asset.setFinAdvice("");  //财务意见
				asset.setFinAdtDate(new DoDate().getStrToDate("1990-1-1")); //审核日期
				asset.setFinAdtName("");  //审核人
				asset.setReturperson(u.getAccperName().trim()); //退回人
				asset.setRetureas(returreas); //退回原因
				asset.setReturtime(dateStr);
				asset.setAduit(2); //终审退回
				
				backFinAuditAll.add(asset);
				
			}
			   this.saveOrUpdateAll(backFinAuditAll);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

		return true;
	}

	public boolean astFirstAuditUpdate(final List<Asset> assetList) throws Exception {
		// TODO Auto-generated method stub
		Boolean flag = (Boolean) this.getHibernateTemplate().execute(
				new HibernateCallback() {

					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						try {
							for (Asset ast : assetList) {
								
								Asset asset = (Asset) session.load(Asset.class,ast.getId());
								asset.setAduit(3);
							//	asset.setRecordDate(new DoDate().getDate(new Date()));
								session.update(asset);
								session.flush();
								session.clear();
							}
							return true;
						} catch (Exception e) {
							e.printStackTrace();
							return false;
						}
					}

				});

		return flag;
	}

}
